package oa.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import oa.bean.QXCD;
import oa.bean.QXJS;
import oa.bean.TDep;
import oa.bean.TSystem;
import oa.bean.TUser;
import oa.service.UserService;
import oa.util.RedisUtil;
import oa.util.StringUtil;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

@Controller
@RequestMapping("/xadmin")
public class AdminController {
	@Resource
	private UserService userService;
	@Resource
	private RedisUtil redisUtil;
	
	@RequestMapping("/adminindex")
	public String adminindex() {
		return "/xadmin/adminindex";
	}
	
	@RequestMapping("/adminlogout")
	public String adminloginout(HttpSession session) {
		session.invalidate();
		SecurityUtils.getSubject().logout();
		return "redirect:/adminlogin";
	}

	@RequestMapping("/listmenu")
	@ResponseBody
	public String listmenu(HttpSession session, Model model) {
		//TUser user = (TUser)session.getAttribute("user");
		//TUser user = (TUser)SecurityUtils.getSubject().getPrincipal();
        String username = SecurityUtils.getSubject().getPrincipal().toString();
        TUser user = userService.login(username);
		model.addAttribute("user", user);
		String jsdm = user.getOskey();
		String jsonString = "";
		String menuKey = jsdm + "-menu";
		if(redisUtil.exists(menuKey)) {
			jsonString = (String)redisUtil.get(menuKey);
		} else {
			Map<String,List<QXCD>> json = new HashMap<String,List<QXCD>>();
			json.put("onemenu", userService.listmenu1(jsdm));
			json.put("twomenu", userService.listmenu2(jsdm));
			jsonString = JSON.toJSONString(json);
			redisUtil.set(menuKey, jsonString);
			redisUtil.expired(menuKey, 60);
		}
		return jsonString;
	}
	
	@RequestMapping("/system/system")
	public String system() {	
		return "/xadmin/system/system";
	}
	
	@RequestMapping("/system/updatesystem")
	@ResponseBody
	public String updatesystem(TSystem sys) {
		if(userService.updatesystem(sys)>0){
			return "更新成功";
		} else {
			return "更新出错";
		}
	}
	
	@RequestMapping("/system/qxmanage")
	public String qxmanage() {
		return "/xadmin/system/qxManage";
	}
	
	@RequiresPermissions("system/qxmanage")
	@RequestMapping("/system/qxmanage-listrole")
	@ResponseBody
	public String qxmanagelistrole() {
		List<QXJS> roles = userService.listrole();
		return JSON.toJSONString(roles);
	}
	
	@RequestMapping("/system/qxmanage-listjsfortree")
	@ResponseBody
	public String qxmanagelistjsfortree(String jsdm, String flag, HttpSession session) {
		return userService.listjsfortree(jsdm, flag);
	}	

	@RequestMapping("/system/qxmanage-listjsforcombox")
	@ResponseBody
	public String qxmanagelistjsforcombox(String jsdm, String flag, HttpSession session) {
		return userService.listjsfortree(jsdm, flag);
	}	

	@RequestMapping("/system/qxmanage-rolewindow")
	public String qxmanagerolewindow(String jsdm, Model model) {
		if(!StringUtil.nullOrBlank(jsdm)) {
			model.addAttribute("obj", userService.getrole(jsdm));
		}
		return "/xadmin/system/roleWindow";
	}	
	
	@RequestMapping("/system/qxmanage-delrole")
	@ResponseBody
	public String qxmanagedelrole(String jsdm) {
		if(!StringUtil.nullOrBlank(jsdm)){
			if(userService.delrole(jsdm)>0) {
				return "删除成功";
			} else {
				return "删除出错";
			}
		} else {
			return "角色代码不能为空";
		}
	}
	
	@RequestMapping("/system/qxmanage-addrole")
	@ResponseBody
	public String qxmanageaddrole(QXJS qxjs) {
		if(userService.addrole(qxjs)>0) {
			return "添加成功";
		} else {
			return "添加出错";
		}
	}

	@RequestMapping("/system/qxmanage-updaterole")
	@ResponseBody
	public String qxmanageupdaterole(QXJS qxjs) {
		if(userService.updaterole(qxjs)>0) {
			return "修改成功";
		} else {
			return "修改出错";
		}
	}

	@RequestMapping("/system/qxmanage-updaterolezt")
	@ResponseBody
	public String qxmanageupdaterolezt(String jsdm, int zt) {
		if(userService.updaterolezt(jsdm, zt)>0) {
			return "修改成功";
		} else {
			return "修改出错";
		}
	}

	@RequestMapping("/system/qxmanage-setrolewindow")
	public String qxmanagesetrolewindow(String jsdm, Model model) {
		model.addAttribute("jsdm", jsdm);
		return "/xadmin/system/setRoleWindow";
	}
	
	@RequestMapping("/system/qxmanage-setrole")
	@ResponseBody
	@Transactional
	public String qxmanagesetrole(String jsdm, String jscd) throws Exception{
		String[] jscds = jscd.split(",");
		if(userService.updaterolecd(jsdm, jscds)>0) {
			return "修改成功";
		} else {
			return "修改出错";
		}
	}
	
	@RequestMapping("/system/usermanage")
	public String usermanage() {
		return "/xadmin/system/userManage";
	}

	@RequiresRoles("admin")
	@RequestMapping("/system/usermanage-listuser")
	@ResponseBody
	public String usermanagelistuser(String username, String fullname, String oskey, Integer deptid) {
		List<TUser> users = userService.listuser(username, fullname, oskey, deptid);
		return JSON.toJSONString(users);
	}

	@RequestMapping("/system/usermanage-userwindow")
	public String usermanageuserwindow(Integer userid, Model model) {
		if(userid != null) {
			TUser user = userService.getuser(userid);
			model.addAttribute("obj", user);
		}
		model.addAttribute("userid", userid);
		return "/xadmin/system/userWindow";
	}

	@RequestMapping("/system/usermanage-deluser")
	@ResponseBody
	public String usermanagedeluser(Integer userid) {
		if(!StringUtil.nullOrBlank(userid)){
			if(userService.deluser(userid)>0) {
				return "删除成功";
			} else {
				return "删除出错";
			}
		} else {
			return "用户代码不能为空";
		}
	}
	
	@RequestMapping("/system/usermanage-adduser")
	@ResponseBody
	public String usermanageadduser(TUser user) {
		if(userService.adduser(user)>0) {
			return "添加成功";
		} else {
			return "添加出错";
		}
	}

	@RequestMapping("/system/usermanage-updateuser")
	@ResponseBody
	public String usermanageupdateuser(TUser user) {
		if(userService.updateuser(user)>0) {
			return "修改成功";
		} else {
			return "修改出错";
		}
	}

	@RequestMapping("/system/usermanage-updateuserzt")
	@ResponseBody
	public String usermanageupdateuserzt(Integer userid, Integer zt) {
		if(userService.updateuserzt(userid, zt)>0) {
			return "修改成功";
		} else {
			return "修改出错";
		}
	}
	
	@RequestMapping("/system/usereditinfo")
	public String usereditinfo(Map<String, Object> model) {
		String username = (String)SecurityUtils.getSubject().getPrincipal();
		TUser user = userService.login(username);
		model.put("obj", user);
		return "/xadmin/system/userEditInfo";
	}

	@RequestMapping("/system/publicinfo")
	public String publicinfo() {
		return "/xadmin/system/publicInfo";
	}

	
	@RequestMapping("/system/deptmanage")
	public String deptmanage() {
		return "/xadmin/system/DeptManage";
	}

	@RequiresRoles("admin")
	@RequestMapping("/system/deptmanage-listdept")
	@ResponseBody
	public String deptmanagelistdept() {
		List<TDep> depts = userService.listdept();
		StringBuffer s = new StringBuffer();
		s.append("{\"rows\":").append(JSON.toJSONString(depts).replaceAll("fid", "_parentId"));
		s.append(",\"total\":").append(depts.size()).append("}");
		return s.toString();
		//return JSON.toJSONString(depts);
	}

	@RequestMapping("/system/deptmanage-deptwindow")
	public String deptmanagedeptwindow(Integer id, Model model) {
		if(id != null) {
			TDep dept = userService.getdept(id);
			model.addAttribute("obj", dept);
		}
		model.addAttribute("id", id);
		return "/xadmin/system/deptWindow";
	}

	@RequestMapping("/system/deptmanage-deldept")
	@ResponseBody
	public String deptmanagedeldept(Integer id) {
		if(!StringUtil.nullOrBlank(id)){
			if(userService.deldept(id)>0) {
				return "删除成功";
			} else {
				return "删除出错";
			}
		} else {
			return "用户代码不能为空";
		}
	}
	
	@RequestMapping("/system/deptmanage-adddept")
	@ResponseBody
	public String deptmanageadddept(TDep dept) {
		if(userService.adddept(dept)>0) {
			return "添加成功";
		} else {
			return "添加出错";
		}
	}

	@RequestMapping("/system/deptmanage-updatedept")
	@ResponseBody
	public String deptmanageupdatedept(TDep dept) {
		if(userService.updatedept(dept)>0) {
			return "修改成功";
		} else {
			return "修改出错";
		}
	}

	@RequestMapping("/system/deptmanage-updatedeptzt")
	@ResponseBody
	public String deptmanageupdatedeptzt(Integer id, Integer zt) {
		if(userService.updatedeptzt(id, zt)>0) {
			return "修改成功";
		} else {
			return "修改出错";
		}
	}
	
	@RequestMapping("/system/deptmanage-listdeptfortree")
	@ResponseBody
	public String deptmanagelistdeptfortree() {
		return userService.listdeptfortree();
	}
	
}
